package JUC.dmeo01;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author mubei
 * @date 2021/11/21
 * @description
 * 真正的多线程开发  降低耦合性
 * 线程就是一个单独的资源类  没有任何附属的操作
 *
 */
public class Test1 {

    public static void main(String[] args) {
        // 获取cpu核数    IO密集型
        //System.out.println(Runtime.getRuntime().availableProcessors());


        Ticket ticket = new Ticket();
        new Thread(()->{
            for (int i = 0; i <40; i++) {
                ticket.sale();
            }

        },"ah").start();
        new Thread(()->{
            for (int i = 0; i <40; i++) {
                ticket.sale();
            }

        },"mu").start();
        new Thread(()->{
            for (int i = 0; i <40; i++) {
                ticket.sale();
            }

        },"mw").start();

    }

}


// 资源类OOP
class Ticket{
    private int number = 50;


    Lock lock = new ReentrantLock();
    // 1.synchronized
    public  void sale(){
        lock.lock();
        try{
            if(number > 0 ){
                System.out.println(Thread.currentThread().getName()+"卖出第"+number--);
            }
        }finally {
            lock.unlock();
        }

    }
}